/*
 * Copyright (C) 2005-2006 Dmitri Maximovich <maxim@md.pp.ru>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

var T = {
  onTooltipShowing: function() {
    var tooltip = document.getElementById('cc-tooltip');
    var box = document.createElement("vbox");
    box.setAttribute("id", "cc-vbox");

	var grid = document.createElement("grid");
	box.appendChild(grid);
	
	var columns = document.createElement("columns");
	columns.appendChild(document.createElement("column")); // name
	columns.appendChild(document.createElement("column")); // status
	columns.appendChild(document.createElement("column")); // date

	grid.appendChild(columns);	
	
	var rows = document.createElement("rows");
	grid.appendChild(rows);	
	
	// header row
	rows.appendChild(T.constructHeader());

    // grab reference to preven on the fly changes
    var servers = ccServers;
    if (servers != null) {
		for(var i = 0; i < servers.length; i++) {
		    var server = servers[i];
            // server label
            var label = document.createElement("label");
            label.setAttribute("value", server.name+(server.error != null ? ": "+server.error : ""));
            label.setAttribute("style", "font-weight: bold;"+(server.error != null ? "color:red;" : ""));
            rows.appendChild(label);
            // project labels
		    for(var j = 0; j < server.projects.length; j++) {
			    var project = server.projects[j];
				rows.	appendChild(T.constructRow(project));
			}
		}
    }

	// show
	tooltip.appendChild(box);
  },
  
  constructHeader: function() {
	var row = document.createElement("row");
	row.setAttribute("style", "text-decoration:underline;");

	var label = document.createElement("label");
	label.setAttribute("value", "Project");
	row.appendChild(label);
	
	label = document.createElement("label");
	label.setAttribute("value", "Status");
	row.appendChild(label);

	label = document.createElement("label");
	label.setAttribute("value", "Since last build");
	row.appendChild(label);

	return row;
  },
  
  constructRow: function(project) {
	const stylePassed = "color:green;"
	const styleFailed = "color:red;"
	const stylePaused = "font-style:italic;";
	
	var row = document.createElement("row");
    row.setAttribute("style", (project.passed ? stylePassed : styleFailed)+(project.paused ? stylePaused : "" ));

	// project name		
	var label = document.createElement("label");
	label.setAttribute("value", project.name);
	row.appendChild(label);

	// status
	label = document.createElement("label");
	label.setAttribute("value", project.status);
	row.appendChild(label);
		
	// date
	label = document.createElement("label");
	
	if (project.passed) {
	  label.setAttribute("value", T.getElapsedTime(project.lastBuild));
	}
	else {
	  label.setAttribute("value", T.getElapsedTime(project.lastBuild)+"/"+T.getElapsedTime(project.lastSuccessfulBuild));
	}
	row.appendChild(label);
		
	return row;
  },
  
  onTooltipHiding: function() {
    var old = document.getElementById('cc-vbox');
    if (old != null) {
	    old.parentNode.removeChild(old);
	}
  },
  
  updateToolbar: function(servers) {
  	var servCount = 0;
  	var totalProjects = 0;
  	var failedProjects = 0;
  	for(var i = 0; i < servers.length; i++) {
  	  var server = servers[i];
  	  if (server.error == null) {
  	      servCount++;
	  	  for(var j = 0; j < server.projects.length; j++) {
	  	    var project = server.projects[j];
	  	    totalProjects++;
	  	    if (!project.passed) {
			  failedProjects++;  	      
	  	    }
	  	  }
	   }
  	}

    var toolbar = document.getElementById('cc-status');
    toolbar.setAttribute("label", servCount+": "+failedProjects+"/"+totalProjects);
    if (failedProjects == 0) {
      toolbar.setAttribute("style", "color:green;");
    }
    else {
      toolbar.setAttribute("style", "color:red;");
    }
  },

  // param d - milliseconds
  getElapsedTime: function(d) {
    var now = (new Date()).getTime();
    var minutesSince = (now - d)/(1000 * 60);

    var daysPassed = Math.floor(minutesSince/(60*24));
    var hoursPassed = Math.floor((minutesSince - (daysPassed * 60 * 24))/60);
    var minutesPassed = Math.floor((minutesSince - (daysPassed * 60 * 24) - (hoursPassed * 60)));

    var result = "";
    if (daysPassed > 0) {
       result = daysPassed + "d "+hoursPassed+"h "+minutesPassed+"m";
    }
    else {
      result = hoursPassed + "h " + minutesPassed+"m";
    }
    return result;
  }

};


